//
//  LGMonitoringSettingController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/3/13.
//  全年体征监测

#import "LGMonitoringSettingController.h"
//编辑
#import "LGCareMonitorEditView.h"
#import "LGCareMonitorEditCell.h"
@interface LGMonitoringSettingController ()<UITableViewDelegate,UITableViewDataSource,LGCareMonitorEditDelegate,LGBottomButtonDelegate>
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)LGBottomButtonView *bottomView;
/**   */
@property (nonatomic , strong)NSMutableArray *dataArray;

@end

@implementation LGMonitoringSettingController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"全年体征监测";
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.bottomView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/Careplan/physicalsDetail" parameters:@{@"patient_careplan_id":self.planid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.dataArray = [NSMutableArray arrayWithArray:returnData];
            [self.tableView reloadData];
        }
    } error:^(NSError *error) {
        
    }];
}

//
-(void)bottomButtonTouched{
    [RequestUtil withPOST:@"/doctor/Careplan/savePatientPhysicals" parameters:@{@"patient_careplan_id":self.planid,@"patient_id":self.patientid,@"physicals":[ObjectTool objectToJson:self.dataArray]} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            [TooltipView showMessage:@"保存成功" offset:0];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [TooltipView showMessage:message offset:0];
        }
    } error:^(NSError *error) {
        [TooltipView showMessage:LGErrorMessage offset:0];
    }];
}


-(void)careMonitorSelect:(BOOL)selected index:(NSInteger)index{
    NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithDictionary:self.dataArray[index]];
    [tempDic setValue:@(selected) forKey:@"selected"];
    [self.dataArray replaceObjectAtIndex:index withObject:tempDic];
    [self.tableView reloadData];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGCareMonitorEditCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dataDic = self.dataArray[indexPath.row];
    cell.index = indexPath.row;
    cell.delegate = self;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    __block NSInteger index = indexPath.row;
    NSDictionary *tempDic = self.dataArray[indexPath.row];
    NSArray *rateArray = tempDic[@"frequencySelect"];
    NSArray *timeArray = tempDic[@"timeSelect"];
    NSInteger lineNum = timeArray.count/3+((timeArray.count%3>0)?1:0);
    CGFloat height = viewPix(120)+bottomSafeBarHeight/4.0+viewPix(100)+viewPix(48)*(rateArray.count+lineNum);
    LGCareMonitorEditView *editView = [[LGCareMonitorEditView alloc]initWithTitle:@"请选择" bottomBtn:@[@{@"title":@"取消",@"color":@"#5E8EF5",@"type":@"bolder"},@{@"title":@"确定",@"color":@"#5E8EF5",@"type":@"back"}] baseHeight:height];
    editView.dataDic = tempDic;
    [[ObjectTool mainWindow] addSubview:editView];
    [editView showViewAnimation];
    __weak typeof(self) weakSelf = self;
    editView.editFinishAction = ^(NSDictionary * _Nonnull tempDic) {
        [weakSelf.dataArray replaceObjectAtIndex:index withObject:tempDic];
        [weakSelf.tableView reloadData];
    };
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight-viewPix(65)-bottomSafeBarHeight/4.0) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGCareMonitorEditCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(10))];
    }
    return _tableView;
}

-(LGBottomButtonView *)bottomView{
    if(!_bottomView){
        _bottomView = [[LGBottomButtonView alloc]initWithTitle:@"确定" color:@"#5E8EF5" type:LGBottomButtonTypeBack frame:CGRectMake(0, kMaxY(self.tableView.frame), Screen_W, viewPix(65)+bottomSafeBarHeight/4.0)];
        _bottomView.delegate = self;
    }
    return _bottomView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
